From: Robert Lipe Date: Sat, 14 Sep 2024 04:18:52 +0000 (-0500) Subject: Various modernizations in humminbird. (#1100) X-Git-Tag: archive/raspbian/1.10.0+ds-2+rpi1~1^2~12^2^2~52 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=806bc39aea05c19de7e80c78b919865964a479b5;p=gpsbabel.git Various modernizations in humminbird. (#1100) * Various modernizations in humminbird. - Make humminbird_icons[] a QStringList. - I don't think the icon finder ever actually worked the way it was described in the comments. - Comment fix in exif - Kill xstrndup (yay) --- diff --git a/.gitignore b/.gitignore index eb7338ca2..0aecc86f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.a +*.cbp *.o -*.swp +*.swo .ninja_deps .ninja_log .qmake.cache @@ -10,6 +11,7 @@ /*.gcno /*.gcov /.cmake/ +/.idea/ /.vs/ /.vscode/ /GPSBabel @@ -27,6 +29,7 @@ /build/ /codacy-clang-tidy /compile_commands.json +/config.tests/ /config.h /config.log /config.status diff --git a/defs.h b/defs.h index 0902154b9..73dd84ed3 100644 --- a/defs.h +++ b/defs.h @@ -922,7 +922,6 @@ void* xmalloc(size_t size); void* xrealloc(void* p, size_t s); void xfree(const void* mem); char* xstrdup(const QString& s); -char* xstrndup(const char* str, size_t sz); char* xstrdup(const char* s); FILE* xfopen(const char* fname, const char* type, const char* errtxt); diff --git a/exif.cc b/exif.cc index 553a4775b..cece53d16 100644 --- a/exif.cc +++ b/exif.cc @@ -63,7 +63,7 @@ #include // for memcmp, strlen #include // for as_const -#include "defs.h" // for Waypoint, fatal, warning, global_options, global_opts, unknown_alt, xfree, route_disp_all, track_disp_all, waypt_disp_all, wp_flags, KNOTS_TO_MPS, KPH_TO_MPS, MPH_TO_MPS, MPS_TO_KPH, WAYPT_HAS, case_ignore_strcmp, waypt_add, xstrdup, xstrndup, fix_2d +#include "defs.h" // for Waypoint, fatal, warning, global_options, global_opts, unknown_alt, xfree, route_disp_all, track_disp_all, waypt_disp_all, wp_flags, KNOTS_TO_MPS, KPH_TO_MPS, MPH_TO_MPS, MPS_TO_KPH, WAYPT_HAS, case_ignore_strcmp, waypt_add, xstrdup, fix_2d #include "garmin_tables.h" // for gt_lookup_datum_index #include "gbfile.h" // for gbfputuint32, gbfputuint16, gbfgetuint16, gbfgetuint32, gbfseek, gbftell, gbfile, gbfclose, gbfcopyfrom, gbfwrite, gbfopen_be, gbfread, gbfrewind, gbfgetflt, gbfgetint16, gbfopen, gbfputc, gbfputflt, gbsize_t, gbfeof, gbfgetdbl, gbfputdbl, gbfile::(anonymous) #include "jeeps/gpsmath.h" // for GPS_Math_WGS84_To_Known_Datum_M diff --git a/humminbird.cc b/humminbird.cc index e6f7505f5..05e379c4a 100644 --- a/humminbird.cc +++ b/humminbird.cc @@ -1,7 +1,7 @@ /* Copyright (C) 2008 Björn Augustsson, oggust@gmail.com Copyright (C) 2008 Olaf Klein, o.b.klein@gpsbabel.org - Copyright (C) 2005-2013 Robert Lipe, robertlipe+source@gpsbabel.org + Copyright (C) 2005-2024 Robert Lipe, robertlipe+source@gpsbabel.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -62,8 +62,6 @@ Still, they're useful in the code as a plain signature. #define WPT_MAGIC2 0x02030024L // New for 2013. No visible diff?! #define RTE_MAGIC 0x03030088L -#define BAD_CHARS "\r\n\t" - /* The hwr data format is records-based, and the records are 36 bytes long. */ struct HumminbirdBase::humminbird_waypt_t { @@ -223,7 +221,8 @@ HumminbirdBase::humminbird_rd_deinit() const void HumminbirdBase::humminbird_read_wpt(gbfile* fin) { - humminbird_waypt_t w{}; + humminbird_waypt_t w{0}; + static_assert(sizeof(w) == 32); if (! gbfread(&w, 1, sizeof(w), fin)) { fatal(MYNAME ": Unexpected end of file!\n"); @@ -248,7 +247,7 @@ HumminbirdBase::humminbird_read_wpt(gbfile* fin) wpt->latitude = geocentric_to_geodetic_hwr(guder); wpt->longitude = static_cast(w.east) / EAST_SCALE * 180.0; - wpt->altitude = 0.0; /* It's from a fishfinder... */ + wpt->altitude = 0.0; /* It's from a fishfinder... */ if (w.depth != 0) { wpt->set_depth(static_cast(w.depth) / 100.0); @@ -284,8 +283,8 @@ HumminbirdBase::humminbird_read_wpt(gbfile* fin) void HumminbirdBase::humminbird_read_route(gbfile* fin) const { - - humminbird_rte_t hrte{}; + humminbird_rte_t hrte{0}; + static_assert(sizeof(hrte) == 132); if (! gbfread(&hrte, 1, sizeof(hrte), fin)) { fatal(MYNAME ": Unexpected end of file!\n"); @@ -302,7 +301,7 @@ HumminbirdBase::humminbird_read_route(gbfile* fin) const /* locate the point over his internal Humminbird "Number" */ if (const Waypoint* wpt = wpt_num_to_wpt_hash.value(hrte.points[i], nullptr); wpt != nullptr) { - if (rte == nullptr) { + if (rte == nullptr) { rte = new route_head; route_add_head(rte); rte->rte_name = QByteArray(hrte.name, static_cast(qstrnlen(hrte.name, sizeof(hrte.name)))); @@ -316,8 +315,8 @@ HumminbirdBase::humminbird_read_route(gbfile* fin) const void HumminbirdBase::humminbird_read_track(gbfile* fin) { - - humminbird_trk_header_t th{}; + humminbird_trk_header_t th{0}; + static_assert(sizeof(th) == 64); if (! gbfread(&th, 1, sizeof(th), fin)) { fatal(MYNAME ": Unexpected end of file reading header!\n"); @@ -424,12 +423,10 @@ HumminbirdBase::humminbird_read_track(gbfile* fin) void HumminbirdBase::humminbird_read_track_old(gbfile* fin) { - - humminbird_trk_header_old_t th{}; + humminbird_trk_header_old_t th{0}; constexpr int file_len = 8048; char namebuf[TRK_NAME_LEN]; - if (! gbfread(&th, 1, sizeof(th), fin)) { fatal(MYNAME ": Unexpected end of file reading header!\n"); } @@ -483,7 +480,7 @@ HumminbirdBase::humminbird_read_track_old(gbfile* fin) first_wpt->altitude = 0.0; track_add_wpt(trk, first_wpt); - for (int i = 0 ; iset_length(WPT_NAME_LEN - 1); - wptname_sh->set_badchars(BAD_CHARS); + wptname_sh->set_badchars(kBadChars); wptname_sh->set_mustupper(false); wptname_sh->set_mustuniq(false); wptname_sh->set_whitespace_ok(true); @@ -572,7 +569,7 @@ HumminbirdBase::humminbird_wr_init(const QString& fname) rtename_sh = new MakeShort; rtename_sh->set_length(RTE_NAME_LEN - 1); - rtename_sh->set_badchars(BAD_CHARS); + rtename_sh->set_badchars(kBadChars); rtename_sh->set_mustupper(false); rtename_sh->set_mustuniq(false); rtename_sh->set_whitespace_ok(true); @@ -581,7 +578,7 @@ HumminbirdBase::humminbird_wr_init(const QString& fname) trkname_sh = new MakeShort; trkname_sh->set_length(RTE_NAME_LEN - 1); - trkname_sh->set_badchars(BAD_CHARS); + trkname_sh->set_badchars(kBadChars); trkname_sh->set_mustupper(false); trkname_sh->set_mustuniq(false); trkname_sh->set_whitespace_ok(true); @@ -609,8 +606,7 @@ HumminbirdBase::humminbird_wr_deinit() void HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt) { - humminbird_waypt_t hum{}; - int num_icons = std::size(humminbird_icons); + humminbird_waypt_t hum{0}; be_write16(&hum.num, waypoint_num++); hum.zero = 0; @@ -618,20 +614,26 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt) hum.icon = 255; // Icon.... - if (!wpt->icon_descr.isNull()) { - for (int i = 0; i < num_icons; i++) { - if (!wpt->icon_descr.compare(humminbird_icons[i], Qt::CaseInsensitive)) { + QString icon = wpt->icon_descr; + if (!icon.isEmpty()) { + int i = 0; + for (const auto& hi : humminbird_icons) { + if (!icon.compare(hi, Qt::CaseInsensitive)) { hum.icon = i; break; } + i++; } - if (hum.icon == 255) { /* no success, no try to find the item in a more comlex name */ + + if (hum.icon == 255) { /* no success, now try to find the item in a more complex name */ hum.icon = 0; /* i.e. "Diamond" as part of "Diamond, Green" or "Green Diamond" */ - for (int i = 0; i < num_icons; i++) { - if (wpt->icon_descr.contains(humminbird_icons[i], Qt::CaseInsensitive)) { + i = 0; + for (const auto& hi : humminbird_icons) { + if (icon.contains(hi, Qt::CaseInsensitive)) { hum.icon = i; break; } + i++; } } } diff --git a/humminbird.h b/humminbird.h index ebc34d9da..9892e9b29 100644 --- a/humminbird.h +++ b/humminbird.h @@ -23,6 +23,7 @@ #include // for QHash #include // for QString +#include // for QString #include // for QVector #include // for int32_t, uint32_t @@ -52,13 +53,16 @@ protected: // constants related to position conversions. static constexpr double i1924_equ_axis = 6378388.0; static constexpr double EAST_SCALE = 20038297.0; /* this is i1924_equ_axis*pi */ + // static constexpr double i1924_polar_axis = 6356911.946; // We use a modified international 1924 ellipse with a different flattening, // defined by cos_ae = cos(angular eccentricity). static constexpr double cos_ae = 0.9966349016452; static constexpr double cos2_ae = cos_ae * cos_ae; - static constexpr const char* humminbird_icons[] = { + static constexpr auto kBadChars = "\r\n\t"; + + const QStringList humminbird_icons = { "Normal", /* 0 */ "House", /* 1 */ "Red cross", /* 2 */ diff --git a/util.cc b/util.cc index 7285c7467..7840d5268 100644 --- a/util.cc +++ b/util.cc @@ -104,27 +104,6 @@ char* xstrdup(const QString& s) return xstrdup(CSTR(s)); } -/* - * Duplicate at most sz bytes in str. - */ -char* -xstrndup(const char* str, size_t sz) -{ - size_t newlen = 0; - const char* cin = str; - - while ((newlen < sz) && (*cin != '\0')) { - newlen++; - cin++; - } - - char* newstr = (char*) xmalloc(newlen + 1); - memcpy(newstr, str, newlen); - newstr[newlen] = 0; - - return newstr; -} - void* xrealloc(void* p, size_t s) {